package com.iwhalecloud.bss.kite.cucc.service.util;

import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Random;

public class DataNodeRandomUtil {

    private static final String DC_PUBLIC_STYPE = "20191021009";

    private static final String DC_PUBLIC_PKEY = "1";

    private static String getRandonInt(int minNum, int maxMun) {
        Random random = new Random();
        int result = random.nextInt(maxMun - minNum + 1) + minNum;
        return String.valueOf(result);
    }

    /**
     *
     * @return
     */
    public static String getRandonDataNode() {
        DcPublic dcPublic = DcPublicCache.get(DC_PUBLIC_STYPE, DC_PUBLIC_PKEY);
        return getRandonDataNode(dcPublic);

    }


    /**
     *
     * @return
     */
    public static String getRandonDataNode(DcPublic dcPublic) {

        if (dcPublic != null) {
            int minNum = NumberUtils.toInt(dcPublic.getCodea());
            int maxNUm = NumberUtils.toInt(dcPublic.getCodeb());
            String dataNodePrefix = dcPublic.getCodec();
            String nodeNum = getRandonInt(minNum, maxNUm);
            //是否要带0
            String isContainZero = dcPublic.getCoded();
            if ("0".equals(isContainZero) && Integer.parseInt(nodeNum) < 10) {
                nodeNum = "0" + nodeNum;
            }

            return dataNodePrefix + "_" + nodeNum;
        }

        return "";


    }

    /**
     *
     * @return
     */
    public static String getRandonDataNodeStr() {
        DcPublic dcPublic = DcPublicCache.get(DC_PUBLIC_STYPE, DC_PUBLIC_PKEY);
        return getRandonDataNodeStr(dcPublic);
    }


    public static String getRandonDataNodeStr(DcPublic dcPublic) {

        if (dcPublic != null) {
            return StringUtils.join("/* !HINT({\"dn\":[\"", getRandonDataNode(dcPublic), "\"]})*/");
        }

        return "";


    }


}
